// Copyright 2009 Google Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// Author: Jamie Nicolson

#include <QtCore/QString>
#include <QtCore/QStringList>

#include "util/common.h"
#include "util/protocol.h"
#include "util/provider.h"

namespace popdiag {

Protocol::Protocol(const QString& protocol_name) :
    protocol_name_(protocol_name) {
}

Protocol::~Protocol() {
}

QString Protocol::Name() const {
  return protocol_name_.toUpper();
}

class ImapProtocol : public Protocol {
 public:
  ImapProtocol() : Protocol("imap") { }

  int SecurePort() const { return IMAP_SSL_PORT; }
  bool HasPlaintextPort() const { return true; }
  int PlaintextPort() const { return IMAP_PLAINTEXT_PORT; }
  bool IsOurMailServer(const QString &hostname) const {
    return provider->IsOurImapServer(hostname);
  }
  bool MustNotUseSpa() const {
    return provider->ImapMustNotUseSpa();
  }
  bool VerifyUsernameFromEmailAddress(const QString &username,
                                      const QString &email) const {
    return provider->ImapVerifyUsernameFromEmailAddress(username, email);
  }
};

class PopProtocol : public Protocol {
 public:
  PopProtocol() : Protocol("pop") { }

  int SecurePort() const { return POP_SSL_PORT; }
  bool HasPlaintextPort() const { return true; }
  int PlaintextPort() const { return POP_PLAINTEXT_PORT; }
  bool IsOurMailServer(const QString &hostname) const {
    return provider->IsOurPopServer(hostname);
  }
  bool MustNotUseSpa() const {
    return provider->PopMustNotUseSpa();
  }
  bool VerifyUsernameFromEmailAddress(const QString &username,
                                      const QString &email) const {
    return provider->PopVerifyUsernameFromEmailAddress(username, email);
  }
};

class SmtpProtocol : public Protocol {
 public:
  SmtpProtocol() : Protocol("smtp") { }

  int SecurePort() const { return SMTP_MSA_SSL_PORT; }
  bool HasPlaintextPort() const { return true; }
  int PlaintextPort() const { return SMTP_MX_PLAINTEXT_PORT; }
  bool IsOurMailServer(const QString &hostname) const {
    return provider->IsOurSmtpServer(hostname);
  }
  bool MustNotUseSpa() const {
    return provider->SmtpMustNotUseSpa();
  }
  bool VerifyUsernameFromEmailAddress(const QString &username,
                                      const QString &email) const {
    return provider->SmtpVerifyUsernameFromEmailAddress(username, email);
  }
};


PopProtocol POP_PROTOCOL;
ImapProtocol IMAP_PROTOCOL;
SmtpProtocol SMTP_PROTOCOL;

const Protocol& Protocol::POP() {
  return POP_PROTOCOL;
}

const Protocol& Protocol::IMAP() {
  return IMAP_PROTOCOL;
}

const Protocol& Protocol::SMTP() {
  return SMTP_PROTOCOL;
}

}  // namespace popdiag
